package com.pengli.mytools.ui.mainPage.collection;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.pengli.mytools.MyApplication;
import com.pengli.mytools.R;
import com.pengli.mytools.beans.po.URLData;
import com.pengli.mytools.greendao.URLDataDao;
import com.pengli.mytools.ui.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddURLActivity extends BaseActivity {

    @BindView(R.id.add_url)
    TextView add;
    @BindView(R.id.url)
    EditText url;
    @BindView(R.id.urlType)
    Spinner urlType;
    @BindView(R.id.urlName)
    EditText urlName;

    URLData entity = new URLData();

    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_url);
        ButterKnife.bind(this);

        url.setText("http://");

        String[] type={"movie","music","doc"};

        // 为下拉列表定义一个适配器，使用到上面定义的turtleList
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, type);
        // 为适配器设置下拉列表下拉时的菜单样式，有好几种样式，请根据喜好选择
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 将适配器添加到下拉列表上
        urlType.setAdapter(adapter);


        urlType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                entity.setUrlType(type[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    protected void initData() {


    }

    @Override
    protected void setListener() {



        add.setOnClickListener(v -> {
            URLDataDao urlDataDao = MyApplication.getInstance().getDaoSession().getURLDataDao();
            entity.setUrl(url.getText().toString().trim());
            entity.setUrlName(urlName.getText().toString().trim());
            entity.setUrlStatus("1");
            long insert = urlDataDao.insert(entity);
            if (insert>0){
                AddURLActivity.this.finish();
            }


        });
    }
}
